package com.lujianfei.filesend

import com.lujianfei.filetransfer.FileTransferManager
import com.lujianfei.filetransfer.MessageFlag
import com.lujianfei.filetransfer.MessageFlagByte
import com.lujianfei.filetransfer.MessageFlagType
import com.lujianfei.filetransfer.messageLenByteToInt
import com.lujianfei.filetransfer.messageLenIntToByte
import org.junit.Test

import org.junit.Assert.*
import java.nio.ByteBuffer
import kotlin.experimental.and
import kotlin.experimental.or

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }

    @Test
    fun test() {
        val ip = "192.168.1.153"
        println(ip.substring(0,ip.lastIndexOf(".")))
        println(ip.substring(ip.lastIndexOf(".")))
    }

    @Test
    fun test_message() {
        val data = buildNameMessage("你大爷的学习资料.jpg")
        val parseList = arrayListOf<Byte>()
        var lastFlag = 0
        var fileNameLen = 0
        var valid = false

        for (idx in data.indices) {
//            val st = String.format("0x%02X ", data[idx])
//            print(st)
            parseList.add(data[idx])
            if (parseList == MessageFlagByte.HeadFlag) { // 解析报头
                lastFlag = MessageFlag.HeadFlag
                parseList.clear()
                continue
            }

            if (lastFlag == MessageFlag.HeadFlag) { // 解析类型
                if (parseList == MessageFlagType.FileName) {
                    lastFlag = MessageFlag.FileNameFlag
                    parseList.clear()
                    continue
                }
            }

            if (lastFlag == MessageFlag.FileNameFlag) { // 解析文件名长度
                if (parseList.size == 2) {
                    lastFlag = MessageFlag.FileNameLenFlag
                    fileNameLen = messageLenByteToInt(parseList)
                    parseList.clear()
                    continue
                }
            }

            if (lastFlag == MessageFlag.FileNameLenFlag) {  // 解析文件名
                if (parseList.size == fileNameLen) {
                    lastFlag = MessageFlag.FileNameEndFlag
                    println("fileName=${String(parseList.toByteArray(), 0, fileNameLen)}")
                    parseList.clear()
                    continue
                }
            }

            if (lastFlag == MessageFlag.FileNameEndFlag) { // 报尾校验
                if (parseList == MessageFlagByte.TailFlag) {
                    valid = true
                    parseList.clear()
                    continue
                }
            }
        }

        println("valid=${valid}")
    }

    private fun buildNameMessage(str:String):List<Byte> {
        val strData = str.toByteArray(charset = Charsets.UTF_8).toList()
        val data = arrayListOf<Byte>()
        data.addAll(MessageFlagByte.HeadFlag)
        data.addAll(MessageFlagType.FileName)
        data.addAll((messageLenIntToByte(strData.size)))
        data.addAll(strData)
        data.addAll(MessageFlagByte.TailFlag)
        return data
    }
}